<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet"  href="/layui/css/layui.css"/>
<meta charset="UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>用户列表</title>
</head>
<body>
<script src="/plugins/jquery/jquery-1.10.2.min.js"></script>
<script  src="/layui/layui.js"></script>
<script src="/plugins/js/common.js"></script>
<form class="layui-form">
	<label class="layui-form-label">用户名</label>
    <div class="layui-input-inline">
        <input type="text" name="userName" id="userName" autocomplete="off"  placeholder="请输入用户名" class="layui-input">
    </div>
    <div class="layui-inline">
			 <button class="layui-btn" data-type="reload" id="searchButton" onclick="return search();">搜索</button>
          </div>
	  <div style="margin-left: 5px;">
	          <br>
	          <button class="layui-btn layuiadmin-btn-replys" onclick="return toAdd();">新增</button>&nbsp;&nbsp;
	          <button class="layui-btn layuiadmin-btn-replys" onclick="return resetPassword();">密码重置</button>&nbsp;&nbsp;
	  </div>        
</form>
<table class="layui-hide" id="userList"  lay-filter="userList"></table>
<script type="text/html" id="userBarAction">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">锁定</a>
</script>
<script type="text/javascript">
var table;
var layer;
layui.use(['table'], function(){
	layer=layui.layer;
	  table = layui.table;
	  tableIns = initTable(table);
	//监听行工具事件
	  table.on('tool(userList)', function(obj){
	    var data = obj.data;
	    //console.log(obj)
	    if(obj.event === 'del'){
	      layer.confirm('真的删除行么', function(index){
// 	        obj.del();
	        layer.close(index);
	      });
	    } else if(obj.event === 'edit'){
	    	toUpdate(data.id);
	    }
	  });
});

function initTable(){
	table.render({
	    elem: '#userList',
	    url:'/user/listData',
	    cellMinWidth: 90,
	    cols:[
		    	[
		    	  {type:'checkbox',field:'id'},	
			      {field:'userName'  ,title: '用户名'},
			      {field:'phone', title: '联系方式'},
			      {field:'realName',  title: '真实名称'},
			      {field:'roleName',title: '角色'},
			      {field:'createDate',title: '注册时间',sort : true,fixed:false,templet:'<div>{{ formatDate(d.createDate)}}</div>'},
			      {field:'state', title: '状态',templet:function(value){
			    	  if(value==1){
			    		  return "锁定";
			    	  }else{
			    		  return "正常";
			    	  }
			      	}
			      },
			      {fixed: 'right', title:'操作', toolbar: '#userBarAction'}
		    	]
	    ],page: true,
	    limit:8,
	    where: {
		    'userName':$("#userName").val()
	    },
	    id: 'reloadTable'
	  });
}


	
	function search(sender){
		var userName = $('#userName');
	      //执行重载
	      table.reload('reloadTable', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	        	userName: userName.val()
	        }
	      });
	      if(!isNull(sender)){
	    	  layMsg(sender,2000);
	      }
	      return false;
	}

	function toAdd(){
		perContent = layer.open({
			         type:2,
			         title: "新增用户",
			         content: '/user/toAddUser',
			         area: ['700px', '420px'],
			         maxmin: true,            //最大化按钮
			         anim:3,                    //动画
			         shade: [0.8, '#393D49'],//遮罩层
			         end: function(){
// 			        	 alert(1);
		            }
// 		            btn: ['提交','关闭'],
		           /* yes:function(index, layero){
		            	var iframeWin = window["layui-layer-iframe" + index];
		            	iframeWin.addUser();
		                layer.close(index);
		                search();
		            },*/
			     });
		      layer.full(perContent);
		     return false;
	}
	
	function toUpdate(id){
		perContent = layer.open({
	         type:2,
	         title: "编辑用户",
	         content: '/user/toUpdateUser?id='+id,
	         area: ['700px', '420px'],
	         maxmin: true,            //最大化按钮
	         anim:3,                    //动画
	         shade: [0.8, '#393D49'],//遮罩层
	         end: function(){
           }
	     });
     layer.full(perContent);
    return false;
}
	
function resetPassword(){
	var checkList = table.checkStatus('reloadTable').data;
	if(checkList.length==0){
		layMsg("请至少选择一条数据",2000);
		return false;
	}
	var msg = ("确认要重置密码吗？");
	layer.confirm(msg, {
		  btn: ['确认','取消'] //按钮
		}, function(){
			var ids="";
			for(var i=0;i<checkList.length;i++){
				if(i>0){
					ids+=",";
				}
				ids+=checkList[i].id;
			}
			var result = ajaxAction('/user/resetPassword',{ids:ids});
			if(result){
				search("操作成功，密码默认为六个1");
			}else{
				 layMsg("操作失败",2000);
			}
		});
	return false;
}	
	
</script>
<style>
.layui-table-cell{
    height:23px;
    line-height:23px;
}
</style>
</body>
</html>